package errorx

import (
	"github.com/pkg/errors"
	"github.com/zeromicro/go-zero/core/logx"
	"google.golang.org/grpc/status"
	"net/http"
)

func HttpErrorHandler(err error) (int, any) {
	caseErr := errors.Cause(err)
	codeError := NewCodeError(400, "")
	if e, ok := caseErr.(*CodeError); ok {
		codeError.Code = e.Code
		codeError.Data = e.Data
		codeError.Msg = e.Msg
	} else {
		if grpcStatus, ok := status.FromError(caseErr); ok { // grpc err错误
			grpcCode := uint32(grpcStatus.Code())
			codeError.Code = int(grpcCode)
			codeError.Data = grpcStatus.Details()
			codeError.Msg = grpcStatus.Message()
		}
	}
	logx.Errorf("%+v", codeError)
	return http.StatusOK, codeError
}
